/////////////////////////////////////////////////////////////////////
//
//  MOE Framework (Meta Object Encapsulation Framework)
//
//  Original Author: Wiksy Wang, 2011
//
/////////////////////////////////////////////////////////////////////

#include "NamedObjectCacheManager.h"

namespace MOE
{

namespace Name
{

NamedObjectCacheManager::NamedObjectCacheManager()
	: _cachedObjectMap()
{
}

NamedObjectCacheManager::~NamedObjectCacheManager()
{
}

void NamedObjectCacheManager::RegisterHandler(const RawByteVector& objName, const CachedObjectHandler& handler)
{
	_cachedObjectMap[objName].push_back(handler);
}

void NamedObjectCacheManager::Handle(const RawByteVector& objName, void* dataPtr)
{
	CachedObjectMap::iterator mapIter = _cachedObjectMap.find(objName);
	if (mapIter == _cachedObjectMap.end())
	{
		return;
	}

	for (HandlerList::const_iterator listIter = mapIter->second.begin(), listEnd = mapIter->second.end(); listIter != listEnd; ++listIter)
	{
		(*listIter)(dataPtr);
	}

	_cachedObjectMap.erase_return_void(mapIter);
}

void NamedObjectCacheManager::ClearAllHandlers()
{
	_cachedObjectMap.clear();
}

bool NamedObjectCacheManager::HasUnhandled() const
{
	return !_cachedObjectMap.empty();
}

}

}
